package by.amakarevich.services.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.springframework.stereotype.Service;

import by.amakarevich.dataaccess.BookingDao;
import by.amakarevich.dataaccess.DeliveryAddressDao;
import by.amakarevich.datamodel.Booking;
import by.amakarevich.datamodel.DeliveryAddress;
import by.amakarevich.datamodel.UserAccount;
import by.amakarevich.services.DeliveryAddressService;


@Service
public class DeliveryAddressServiceImpl implements DeliveryAddressService {
	@Inject
	private DeliveryAddressDao deliveryAddressDao;
	
	@Inject
	private BookingDao bookingDao;
	
	@Override
	public void addNewDeliveryAddress(DeliveryAddress deliveryAddress,  UserAccount userAccount, Booking booking) {
		booking.setUserAccount(userAccount);
		bookingDao.insert(booking);
		deliveryAddress.setUserAccount(userAccount);
		deliveryAddress.setBooking(booking);
		deliveryAddressDao.insert(deliveryAddress);
	}

	@Override
	public DeliveryAddress get(Long id) {
		return deliveryAddressDao.getById(id);
	}

	@Override
	public void updateDeliveryAddress(DeliveryAddress deliveryAddress) {
		deliveryAddressDao.update(deliveryAddress);
	}

	@Override
	public void deleteDeliveryAddress(Long id) {
		deliveryAddressDao.delete(id);
		bookingDao.delete(id);
	}
	  
	@Override
	    public void deleteAll() {
		deliveryAddressDao.deleteAll();
	}
	
    public List<DeliveryAddress> getAllDeliveryAddressByUser(SingularAttribute<DeliveryAddress, ?> attr, boolean ascending, int startRecord, int pageSize){
    	return deliveryAddressDao.getAllDeliveryAddressByUser(attr, ascending, startRecord, pageSize);
    }
    
    @Override
    public DeliveryAddress getByBooking(Booking booking){
    	return deliveryAddressDao.getByBooking(booking);
    }
}
